package cn.ai.boot.config.PCF;

import cn.ai.boot.PcmDataFieldConstant;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jie.zhao@dms365.com
 * @Date 2025/7/25 14:30
 */
public class Bppc0405DataSyncServiceImpl {
    private static Map<String, Object> createDataMap(String path) {
        Map<String, Object> map = new HashMap<>();
        map.put(PcmDataFieldConstant.PCM_COMPONENT_PATH, path);
        map.put(PcmDataFieldConstant.PCM_SKEY, path);
        return map;
    }

    public static void main(String[] args) {

        List<Map<String, Object>> groupList = new ArrayList<>();
        groupList.add(createDataMap("cwr-100/b1_01/3_164/w2"));
        groupList.add(createDataMap("cwr-100/b1_03/3_164"));
        groupList.add(createDataMap("cwr-100/b1_02/3_164"));
        groupList.add(createDataMap("cwr-100/b1_03/3_164/w1"));

        // 自定义排序
        groupList.sort((o1, o2) -> {
            String path1 = MapUtils.getString(o1, PcmDataFieldConstant.PCM_COMPONENT_PATH, StringUtils.EMPTY);
            String path2 = MapUtils.getString(o2, PcmDataFieldConstant.PCM_COMPONENT_PATH, StringUtils.EMPTY);

            int[] key1 = SortKeyExtractor.extractSortKey(path1);
            int[] key2 = SortKeyExtractor.extractSortKey(path2);

            // 按顺序比较三个字段
            int cmp;
            // 比较第一个字段
            cmp = Integer.compare(key1[0], key2[0]);
            if (cmp != 0) {
                return cmp;
            }
            // 比较第二个字段
            cmp = Integer.compare(key1[1], key2[1]);
            if (cmp != 0) {
                return cmp;
            }
            // 比较第三个字段
            return Integer.compare(key1[2], key2[2]);
        });
        System.out.println("\n排序后:");
        groupList.forEach(map ->
                System.out.println(MapUtils.getString(map, PcmDataFieldConstant.PCM_COMPONENT_PATH, StringUtils.EMPTY)));
    }
}
